package com.example.administrator.yanfoxconn.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TableRow;
import android.widget.TextView;

import com.example.administrator.yanfoxconn.R;
import com.example.administrator.yanfoxconn.adapter.GTCheckListAdapter;
import com.example.administrator.yanfoxconn.adapter.LMExceListAdapter;
import com.example.administrator.yanfoxconn.bean.LCMessage;
import com.example.administrator.yanfoxconn.bean.LMIE8S;
import com.example.administrator.yanfoxconn.constant.Constants;
import com.example.administrator.yanfoxconn.constant.FoxContext;
import com.example.administrator.yanfoxconn.utils.BaseActivity;
import com.example.administrator.yanfoxconn.utils.ChangeTextUtils;
import com.example.administrator.yanfoxconn.utils.FileUtil;
import com.example.administrator.yanfoxconn.utils.HttpConnectionUtil;
import com.example.administrator.yanfoxconn.utils.HttpUtils;
import com.example.administrator.yanfoxconn.utils.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Description IE8S 稽核異常記錄 列表
 * @Author song
 * @Date 2022/2/19 10:20
 */
public class LMExceListActivity extends BaseActivity implements View.OnClickListener {
    private final int MESSAGE_SET_TEXT = 1;//掃描成功賦值
    private final int MESSAGE_TOAST = 2;//掃描失敗彈出框
    private final int MESSAGE_UP = 3;//提交信息回復
    private final int MESSAGE_NETMISTAKE = 5;//網絡錯誤

    private List<LMIE8S> lmMessages;//項目信息,簽核主管
    private LMExceListAdapter lmExceListAdapter;//列表適配器

    @BindView(R.id.btn_title_left)
    Button btnBack;//返回
    @BindView(R.id.tv_title)
    TextView tvTitle;//標題

    @BindView(R.id.lv_process)
    ListView lvProcess;//進度列表

    private String depart,product,flag1;//网络获取结果
    private String zxType="",zxYear="";//网络获取结果
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lm_exce);
        ButterKnife.bind(this);

        tvTitle.setText("異常列表");
        btnBack.setOnClickListener(this);
        depart=getIntent().getStringExtra("depart");
        product=getIntent().getStringExtra("product");
        flag1=getIntent().getStringExtra("flag1");

        if (flag1.equals("jcjh")){}else{
            zxType=getIntent().getStringExtra("zx_type");
            zxYear=getIntent().getStringExtra("zx_year");
        }
        lvProcess.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Intent intent = new Intent(LMExceListActivity.this,LMExceDetailActivity.class);
                intent.putExtra("msg",(Serializable)lmMessages.get(position));
                intent.putExtra("from","list");
                intent.putExtra("flag1",flag1);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_title_left:
                finish();
                break;
        }
    }


    /*IE8S 獲取數據*/
    public void getList(String product,String depart,String flag1,String sign_type) {

        showDialog();
        final String url = Constants.HTTP_LM_GET_NUMBER_LIST  ;

        JsonObject object = new JsonObject();
        object.addProperty("product", product);
        object.addProperty("depart", depart);
        object.addProperty("flag1", flag1);
        object.addProperty("status", sign_type);

        //開啟一個新執行緒，向伺服器上傳資料
        new Thread() {
            public void run() {
                //把网络访问的代码放在这里
                try {
                    Log.e("---------", "==fff===" + url);
                    String result = HttpConnectionUtil.doPostJsonObject(url, object);
                    Gson gson = new Gson();
                    Log.e("---------", "object==fff===" + object.toString());
                    if (result != null) {
                        dismissDialog();
                        Log.e("---------", "result==fff===" + result);
                        JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                        String errCode = jsonObject.get("code").getAsString();
                        if (errCode.equals("200")) {
                            JsonObject jsonObjectData = jsonObject.get("data").getAsJsonObject();
                            JsonArray array = jsonObjectData.get("getNumbersList1").getAsJsonArray();
                            lmMessages = new ArrayList<LMIE8S>();
                            for (JsonElement type : array) {
                                LMIE8S humi = gson.fromJson(type, LMIE8S.class);
                                lmMessages.add(humi);
                            }
                            if (flag1.equals("jcjh")){
                                JsonArray array2 = jsonObjectData.get("getNumbersList2").getAsJsonArray();
                                for (JsonElement type2 : array2) {
                                    LMIE8S humi2 = gson.fromJson(type2, LMIE8S.class);
                                    lmMessages.add(humi2);
                                }
                            }
                            Message message = new Message();
                            message.what = MESSAGE_SET_TEXT;
                            message.obj = jsonObject.get("message").getAsString();
                            mHandler.sendMessage(message);
                        } else{
                            Message message = new Message();
                            message.what = Constants.MESSAGE_TOAST;
                            message.obj = jsonObject.get("message").getAsString();
                            mHandler.sendMessage(message);
                        }
                    }else{
                        Message message = new Message();
                        message.what = Constants.MESSAGE_NETMISTAKE;
                        mHandler.sendMessage(message);
                        finish();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    /*IE8S 獲取數據*/
    public void getJDList(String product,String depart,String flag1,String zxType,String zxYear) {

        showDialog();
        final String url = Constants.HTTP_LM_GET_JD_NUMBER_LIST  ;

        JsonObject object = new JsonObject();
        object.addProperty("product", product);
        object.addProperty("depart", depart);
        object.addProperty("flag1", flag1);
        object.addProperty("zx_type", zxType);
        object.addProperty("zx_year", zxYear);

        //開啟一個新執行緒，向伺服器上傳資料
        new Thread() {
            public void run() {
                //把网络访问的代码放在这里
                try {
                    Log.e("---------", "==fff===" + url);
                    String result = HttpConnectionUtil.doPostJsonObject(url, object);
                    Gson gson = new Gson();
                    Log.e("---------", "object==fff===" + object.toString());
                    if (result != null) {
                        dismissDialog();
                        Log.e("---------", "result==fff===" + result);
                        JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                        String errCode = jsonObject.get("code").getAsString();
                        if (errCode.equals("200")) {
                            JsonObject jsonObjectData = jsonObject.get("data").getAsJsonObject();
                            JsonArray array = jsonObjectData.get("getNumbersList1").getAsJsonArray();
                            lmMessages = new ArrayList<LMIE8S>();
                            for (JsonElement type : array) {
                                LMIE8S humi = gson.fromJson(type, LMIE8S.class);
                                lmMessages.add(humi);
                            }
                            if (flag1.equals("jcjh")){
                                JsonArray array2 = jsonObjectData.get("getNumbersList2").getAsJsonArray();
                                for (JsonElement type2 : array2) {
                                    LMIE8S humi2 = gson.fromJson(type2, LMIE8S.class);
                                    lmMessages.add(humi2);
                                }
                            }
                            Message message = new Message();
                            message.what = MESSAGE_SET_TEXT;
                            message.obj = jsonObject.get("message").getAsString();
                            mHandler.sendMessage(message);
                        } else{
                            Message message = new Message();
                            message.what = Constants.MESSAGE_TOAST;
                            message.obj = jsonObject.get("message").getAsString();
                            mHandler.sendMessage(message);
                        }
                    }else{
                        Message message = new Message();
                        message.what = Constants.MESSAGE_NETMISTAKE;
                        mHandler.sendMessage(message);
                        finish();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_TOAST://Toast彈出
                    aboutAlert(msg.obj.toString(),MESSAGE_TOAST);
                    if (lmMessages!=null&&lmMessages.size()>0){
                        lmMessages.clear();
                    }
                    dismissDialog();
                    lmExceListAdapter = new LMExceListAdapter(LMExceListActivity.this,lmMessages);
                    lvProcess.setAdapter(lmExceListAdapter);
                    break;
                case MESSAGE_NETMISTAKE://Toast彈出
                    ToastUtils.showLong(LMExceListActivity.this,R.string.net_mistake);
                    break;
                case MESSAGE_UP://提交響應
                    worningAlert(msg.obj.toString(),MESSAGE_UP);
                case MESSAGE_SET_TEXT://進度賦值
                    dismissDialog();
                    lmExceListAdapter = new LMExceListAdapter(LMExceListActivity.this,lmMessages);
                    lvProcess.setAdapter(lmExceListAdapter);
                    break;
            }
            super.handleMessage(msg);
        }
    };

    private void aboutAlert(String msg, final int type) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息")
                .setMessage(msg)
                .setCancelable(false)
                .setPositiveButton("確認", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        // TODO Auto-generated method stub
                        if (type==MESSAGE_TOAST){
                            dismissDialog();
                        }

                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }
    private void worningAlert(String msg, final int type) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息")
                .setMessage(msg)
                .setCancelable(false)
                .setPositiveButton("確認", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        // TODO Auto-generated method stub
                        if (type==MESSAGE_TOAST){

                        }
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (flag1.equals("jcjh")){
            getList(product,depart,flag1,"未整改");
        }else{
            getJDList(product, depart, flag1, zxType, zxYear);
        }
    }
}
